//+------------------------------------------------------------------+
//| 断水流指标 - 调试版本                                               |
//| 目的：找出线条不连续的根本原因                                       |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   2

#property indicator_label1  "Test Line"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_width1  2

#property indicator_label2  "MA Line"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_width2  1

// 输入参数
input int TestPeriod = 14;

// 缓冲区
double TestBuffer[];     // 测试线
double MABuffer[];       // 移动平均线
double TempBuffer[];     // 临时计算缓冲区

//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0, TestBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, MABuffer, INDICATOR_DATA);
   SetIndexBuffer(2, TempBuffer, INDICATOR_CALCULATIONS);
   
   // 设置为时间序列
   ArraySetAsSeries(TestBuffer, true);
   ArraySetAsSeries(MABuffer, true);
   ArraySetAsSeries(TempBuffer, true);
   
   return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
// 简化的MA计算函数
double SimpleMA(const double &array[], int period, int shift)
{
   if(shift < 0 || period <= 0) return 0.0;
   
   int arraySize = ArraySize(array);
   if(shift >= arraySize) return 0.0;
   
   // 检查是否有足够数据
   int available = arraySize - shift;
   if(available < period) period = available;
   if(period <= 0) return 0.0;
   
   double sum = 0.0;
   for(int i = 0; i < period; i++)
   {
      if(shift + i < arraySize)
         sum += array[shift + i];
   }
   
   return sum / period;
}

//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   if(rates_total < TestPeriod + 5) return 0;
   
   int limit = prev_calculated == 0 ? rates_total - 1 : rates_total - prev_calculated;
   if(prev_calculated > 0) limit++;
   
   Print("=== 调试信息 ===");
   Print("rates_total=", rates_total, ", prev_calculated=", prev_calculated, ", limit=", limit);
   
   // 第一步：简单测试 - 直接使用收盘价
   for(int i = limit; i >= 0; i--)
   {
      TestBuffer[i] = close[i];  // 直接复制收盘价
      TempBuffer[i] = close[i];  // 复制到临时缓冲区
   }
   
   // 第二步：计算简单移动平均
   for(int i = limit; i >= 0; i--)
   {
      MABuffer[i] = SimpleMA(TempBuffer, TestPeriod, i);
   }
   
   // 调试输出前几个值
   if(prev_calculated == 0)
   {
      Print("=== 前10个数据点检查 ===");
      for(int j = 0; j < MathMin(10, rates_total); j++)
      {
         Print("Bar[", j, "] close=", DoubleToString(close[j], 5), 
               " test=", DoubleToString(TestBuffer[j], 5),
               " ma=", DoubleToString(MABuffer[j], 5));
      }
      
      // 检查数据连续性
      Print("=== 连续性检查 ===");
      bool isConnected = true;
      for(int j = 1; j < MathMin(10, rates_total); j++)
      {
         double diff = MathAbs(TestBuffer[j] - TestBuffer[j-1]);
         if(diff > close[j] * 0.1) // 如果差异超过10%
         {
            isConnected = false;
            Print("发现不连续点: Bar[", j-1, "]=", TestBuffer[j-1], " Bar[", j, "]=", TestBuffer[j]);
         }
      }
      Print("数据连续性: ", (isConnected ? "正常" : "异常"));
   }
   
   return rates_total;
}

//+------------------------------------------------------------------+